package com.itbaizhan.openhis.controller.doctor;

import cn.hutool.core.bean.BeanUtil;
import com.itbaizhan.openhis.domain.*;
import com.itbaizhan.openhis.dto.PatientDto;
import com.itbaizhan.openhis.service.CareHistoryService;
import com.itbaizhan.openhis.service.PatientService;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.dubbo.config.annotation.Reference;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

//患者信息
@RestController
@RequestMapping("docker/patient")
public class PatientController {
    @Reference
    private PatientService patientService;

    @Reference
    private CareHistoryService careHistoryService;
    /**
     * 分页查询患者信息
     * @param patientDto
     * @return
     */
    @GetMapping("listPatientForPage")
    public AjaxResult listPatientForPage(PatientDto patientDto){
        DataGridView dataGridView = patientService.listPatientForPage(patientDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }
    /**
     * 根据患者id查询患者信息
     */
    @GetMapping("getPatientById/{patientId}")
    public AjaxResult  getPatientById(@PathVariable String patientId){
        Patient patient = patientService.getPatientById(patientId);
        return AjaxResult.success(patient);
    }

    /**
     * 根据患者id查询患者档案
     * @param patientId
     * @return
     */
    @GetMapping("getPatientFileById/{patientId}")
    public AjaxResult  getPatientFileById(@PathVariable String patientId){
        PatientFile patientFile = patientService.getPatientFileById(patientId);
        return AjaxResult.success(patientFile);
    }


    /**
     * 根据患者id查询患者信息、患者档案、患者病历
     * @param patientId
     * @return
     */
    @GetMapping("getPatientAllMessageByPatientId/{patientId}")
    public AjaxResult getPatientAllMessageByPatientId(@PathVariable String patientId){
        //查询患者病历信息
        List<CareHistory> careHistoryList = careHistoryService.queryCareHistoryByPatientId(patientId);
        //构造返回的数据对象
        List<Map<String,Object>> res = new ArrayList<>();
        //病历表下有处方，处方下有处方详情
       for (CareHistory careHistory : careHistoryList) {
            Map<String,Object> careHistoryMap = BeanUtil.beanToMap(careHistory);//转为map
           careHistoryMap.put("careOrders", Collections.EMPTY_LIST); //处方设置一个空集合
           List<Map<String,Object>> reCareOrders = new ArrayList<>();
           //根据病历id查询处方列表,再通过处方id查询处方详情
           List<CareOrder> careOrders = careHistoryService.queryCareOrdersByChId(careHistory.getChId());
           for (CareOrder careOrder : careOrders) {
               //根据处方id查处方详情
               Map<String,Object> careOrderMap = BeanUtil.beanToMap(careOrder);
               List<CareOrderItem> careOrderItems = careHistoryService.queryCareOrderItemsByCoId(careOrder.getCoId());
                //存入map
               careOrderMap.put("careOrderItems",careOrderItems);
               //放入上面的list集合
               reCareOrders.add(careHistoryMap);
           }
           //把所有处方都放进病历列表
           careHistoryMap.put("careOrder",reCareOrders);
           res.add(careHistoryMap);
        }
       return AjaxResult.success(res);
    }

}
